package com.bkgy.modular.tbdsjc621thickresult.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.calsidata.entity.TbEdge;
import com.bkgy.modular.datacollect_bx.bean.JDataSave621;
import com.bkgy.modular.tbdsjc621thickresult.dao.TbDsjC621thickResultDao;
import com.bkgy.modular.tbdsjc621thickresult.entity.*;
import com.bkgy.modular.tbdsjc621thickresult.service.TbDsjC621thickResultService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtils;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 硅钢厚度1720产线厚度结果评价表接口具体实现类
 *
 * @author 生成器生成
 * @date 2022-08-17 15:16:28
 */
@Service
public class TbDsjC621thickResultServiceImpl implements TbDsjC621thickResultService {

    @Autowired
    private TbDsjC621thickResultDao tbDsjC621thickResultDao;

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 添加记录
     *
     * @param data   json字符串
     * @param userId 用户id
     * @param sname  用户姓名
     */
    public ResultData insertDataTbDsjC621thickResult(String data, Long userId, String sname) {
        try {
            JTbDsjC621thickResult jtbDsjC621thickResult = JSON.parseObject(data, JTbDsjC621thickResult.class);
            TbDsjC621thickResult tbDsjC621thickResult = jtbDsjC621thickResult.getTbDsjC621thickResult();
            //CodiUtil.newRecord(userId,sname,tbDsjC621thickResult);
            tbDsjC621thickResultDao.insertDataTbDsjC621thickResult(tbDsjC621thickResult);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null);
        }
    }

    /**
     * 根据主键删除对象
     *
     * @param indocno 对象主键
     */
    public ResultData deleteDataTbDsjC621thickResultOne(Long indocno) {
        try {
            tbDsjC621thickResultDao.deleteDataTbDsjC621thickResultOne(indocno);
            return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 根据主键字符串删除多个对象
     *
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbDsjC621thickResultMany(String str_id) {
        try {
            String sql = "delete tb_dsj_c621thick_result where indocno in(" + str_id + ")";
            tbDsjC621thickResultDao.deleteDataTbDsjC621thickResultMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 修改记录
     *
     * @param userId 用户id
     * @param sname  用户姓名
     * @param data   sysUser 对象实体
     */
    public ResultData updateDataTbDsjC621thickResult(String data, Long userId, String sname) {
        try {
            JTbDsjC621thickResult jtbDsjC621thickResult = JSON.parseObject(data, JTbDsjC621thickResult.class);
            TbDsjC621thickResult tbDsjC621thickResult = jtbDsjC621thickResult.getTbDsjC621thickResult();
            //CodiUtil.editRecord(userId,sname,tbDsjC621thickResult);
            tbDsjC621thickResultDao.updateDataTbDsjC621thickResult(tbDsjC621thickResult);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 分页查看记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjC621thickResultByPage(String data) {
        try {
            JTbDsjC621thickResult jtbDsjC621thickResult = JSON.parseObject(data, JTbDsjC621thickResult.class);
            JSONObject jsonObject = null;
            Integer pageIndex = jtbDsjC621thickResult.getPageIndex();
            Integer pageSize = jtbDsjC621thickResult.getPageSize();

            if (null == pageIndex || null == pageSize) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }

            if (null != jtbDsjC621thickResult.getCondition()) {
                jsonObject = JSON.parseObject(jtbDsjC621thickResult.getCondition().toString());
            }

            String coil_id = null;
            if (StringUtils.hasText(jsonObject.getString("coil_id"))) {
                coil_id = jsonObject.getString("coil_id");
            }

            // 热卷号
            String hot_id = null;
            if (StringUtils.hasText(jsonObject.getString("hot_coil_id"))) {
                hot_id = jsonObject.getString("hot_coil_id");
            }

            String dbegin = null;
            if (!StringUtils.isEmpty(jsonObject.get("dbegin"))) {
                dbegin = jsonObject.get("dbegin").toString();
            }
            String dend = null;
            if (!StringUtils.isEmpty(jsonObject.get("dend"))) {
                dend = jsonObject.get("dend").toString();
            }

            String ex_thk1 = null;  //产品厚度----开始
            if (!StringUtils.isEmpty(jsonObject.get("ex_thk1"))) {
                ex_thk1 = jsonObject.get("ex_thk1").toString();
            }

            String ex_thk2 = null;  //产品厚度---结束
            if (!StringUtils.isEmpty(jsonObject.get("ex_thk2"))) {
                ex_thk2 = jsonObject.get("ex_thk2").toString();
            }

            String en_wid1 = null;  //原料宽度----开始
            if (!StringUtils.isEmpty(jsonObject.get("en_wid1"))) {
                en_wid1 = jsonObject.get("en_wid1").toString();
            }


            String en_wid2 = null;  //原料宽度----结束
            if (!StringUtils.isEmpty(jsonObject.get("en_wid2"))) {
                en_wid2 = jsonObject.get("en_wid2").toString();
            }

            String en_thk1 = null;  //原料厚度----开始
            if (!StringUtils.isEmpty(jsonObject.get("en_thk1"))) {
                en_thk1 = jsonObject.get("en_thk1").toString();
            }

            String en_thk2 = null;  //原料厚度----结束
            if (!StringUtils.isEmpty(jsonObject.get("en_thk2"))) {
                en_thk2 = jsonObject.get("en_thk2").toString();
            }

            String ele_si1 = null;  //硅含量---开始
            if (!StringUtils.isEmpty(jsonObject.get("ele_si1"))) {
                ele_si1 = jsonObject.get("ele_si1").toString();
            }
            String ele_si2 = null;  //硅含量---结束
            if (!StringUtils.isEmpty(jsonObject.get("ele_si2"))) {
                ele_si2 = jsonObject.get("ele_si2").toString();
            }

            String steelgrade = null;  //钢种
            if (!StringUtils.isEmpty(jsonObject.get("steelgrade"))) {
                steelgrade = jsonObject.get("steelgrade").toString();
            }

            String shift = null;
            if (!StringUtils.isEmpty(jsonObject.get("shift"))) {
                shift = jsonObject.get("shift").toString();
            }

            String crew = null;
            if (!StringUtils.isEmpty(jsonObject.get("crew"))) {
                crew = jsonObject.get("crew").toString();
            }

            List<TbDsjC621thickResult> list = tbDsjC621thickResultDao.findDataTbDsjC621thickResultByPage((pageIndex - 1) * pageSize, pageIndex * pageSize, dbegin, dend, ex_thk1, ex_thk2, en_wid1, en_wid2, en_thk1, en_thk2, ele_si1, ele_si2, steelgrade,shift,crew,coil_id,hot_id);
            Integer count = tbDsjC621thickResultDao.findDataTbDsjC621thickResultByPageSize(dbegin, dend, ex_thk1, ex_thk2, en_wid1, en_wid2, en_thk1, en_thk2, ele_si1, ele_si2, steelgrade,shift,crew,coil_id,hot_id);


            Steel_gui steel_gui = new Steel_gui();
            Double thk_head_tole_max_value = 0.0; //头部超差最大值
            String b_coil_id_max = null;   //头部超差最大值 卷号
            String b_hot_coil_id_max = null;   //头部超差最大值 热卷号

            Double thk_head_tole_min_value = 0.0; //头部超差最小值
            String b_coil_id_min = "";   //头部超差最小值 卷号
            String b_hot_coil_id_min = "";   //头部超差最小值 热卷号

            Double thk_tail_tole_max_value = 0.0; //尾部超差最大值
            String e_coil_id_max = "";   //尾部超差最大值 卷号
            String e_hot_coil_id_max = "";   //尾部超差最大值 热卷号

            Double thk_tail_tole_min_value = 0.0; //尾部超差最小值
            String e_coil_id_min = "";   //尾部超差最小值 卷号
            String e_hot_coil_id_min = "";   //尾部超差最小值 热卷号

            for (TbDsjC621thickResult entity : list) {
                if (entity.getThk_head_tole_max_value() != null && entity.getThk_head_tole_max_value() > thk_head_tole_max_value) {
                    thk_head_tole_max_value = entity.getThk_head_tole_max_value();
                    b_coil_id_max = entity.getCoil_id();
                    b_hot_coil_id_max = entity.getHot_coil_id();
                }

                if (entity.getThk_head_tole_min_value() != null && entity.getThk_head_tole_min_value() > thk_head_tole_min_value) {
                    thk_head_tole_min_value = entity.getThk_head_tole_min_value();
                    b_coil_id_min = entity.getCoil_id();
                    b_hot_coil_id_min = entity.getHot_coil_id();
                }
                if (entity.getThk_head_tole_max_value() != null && entity.getThk_head_tole_max_value() > thk_tail_tole_max_value) {
                    thk_tail_tole_max_value = entity.getThk_head_tole_max_value();
                    e_coil_id_max = entity.getCoil_id();
                    e_hot_coil_id_max = entity.getHot_coil_id();
                }
                if (entity.getThk_head_tole_min_value() != null && entity.getThk_head_tole_min_value() > thk_tail_tole_min_value) {
                    thk_tail_tole_min_value = entity.getThk_head_tole_min_value();
                    e_coil_id_min = entity.getCoil_id();
                    e_hot_coil_id_min = entity.getHot_coil_id();
                }


            }

            steel_gui.setThk_head_tole_max_value(thk_head_tole_max_value);
            steel_gui.setB_coil_id_max(b_coil_id_max);
            steel_gui.setB_hot_coil_id_max(b_hot_coil_id_max);
            steel_gui.setThk_head_tole_min_value(thk_head_tole_min_value);
            steel_gui.setB_coil_id_min(b_coil_id_min);
            steel_gui.setB_hot_coil_id_min(b_hot_coil_id_min);
            steel_gui.setThk_tail_tole_max_value(thk_tail_tole_max_value);
            steel_gui.setE_coil_id_max(e_coil_id_max);
            steel_gui.setE_hot_coil_id_max(e_hot_coil_id_max);
            steel_gui.setThk_tail_tole_min_value(thk_tail_tole_min_value);
            steel_gui.setE_coil_id_min(e_coil_id_min);
            steel_gui.setE_hot_coil_id_min(e_hot_coil_id_min);

            ///////////////////////////////  头部超差总长度、头部超差平均长度、尾部超差总长度、尾部超差平均长度
            // 不太清楚规则，还没算
            //用绝对长度开始算
            //头部超差总长度
            Double head_all = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_head_tole_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_head_tole_abs_len).sum();
            //头部超差平均长度
            Double head_avg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_head_tole_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_head_tole_abs_len).average().orElse(0.0);
            Double tail_all = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_tail_tole_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_tail_tole_abs_len).sum();
            Double tail_avg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_tail_tole_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_tail_tole_abs_len).average().orElse(0.0);
            steel_gui.setHead_all(head_all);
            steel_gui.setHead_avg(head_avg);
            steel_gui.setTail_all(tail_all);
            steel_gui.setTail_avg(tail_avg);
            //各个等级总长度(绝对长度计算)
            Double aLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_a_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_a_abs_len).sum();
            Double bLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_b_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_b_abs_len).sum();
            Double cLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_c_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_c_abs_len).sum();
            Double dLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_d_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_d_abs_len).sum();
            Double eLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_e_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_e_abs_len).sum();
            Double fLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_f_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_f_abs_len).sum();
            Double gLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_g_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_g_abs_len).sum();
            Double hLengthSum = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_h_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_h_abs_len).sum();

            steel_gui.setALengthSum(aLengthSum);
            steel_gui.setBLengthSum(bLengthSum);
            steel_gui.setCLengthSum(cLengthSum);
            steel_gui.setDLengthSum(dLengthSum);
            steel_gui.setELengthSum(eLengthSum);
            steel_gui.setFLengthSum(fLengthSum);
            steel_gui.setGLengthSum(gLengthSum);
            steel_gui.setHLengthSum(hLengthSum);
            //各个等级均值(绝对长度计算)
            Double aLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_a_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_a_abs_len).average().orElse(0.0);
            Double bLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_b_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_b_abs_len).average().orElse(0.0);
            Double cLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_c_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_c_abs_len).average().orElse(0.0);
            Double dLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_d_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_d_abs_len).average().orElse(0.0);
            Double eLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_e_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_e_abs_len).average().orElse(0.0);
            Double fLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_f_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_f_abs_len).average().orElse(0.0);
            Double gLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_g_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_g_abs_len).average().orElse(0.0);
            Double hLengthAvg = list.stream().filter(x -> !StringUtils.isEmpty(x.getThk_h_abs_len())).mapToDouble(TbDsjC621thickResult::getThk_h_abs_len).average().orElse(0.0);

            steel_gui.setALengthAvg(aLengthAvg);
            steel_gui.setBLengthAvg(bLengthAvg);
            steel_gui.setCLengthAvg(cLengthAvg);
            steel_gui.setDLengthAvg(dLengthAvg);
            steel_gui.setELengthAvg(eLengthAvg);
            steel_gui.setFLengthAvg(fLengthAvg);
            steel_gui.setGLengthAvg(gLengthAvg);
            steel_gui.setHLengthAvg(hLengthAvg);

            //统计所有卷号
//            List<String> volNos = list.stream().map(TbDsjC621thickResult::getCoil_id).collect(Collectors.toList());
//            List<SiData3> siData3s = findThickByVolumeNo621Si3(volNos);
//            steel_gui.setListSi3(siData3s);

            return ResultData.ResultDataSuccess(list, count, steel_gui);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 列表导出
     * @param jsonObject
     * @param response
     */
    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        String coil_id = null;
        if (StringUtils.hasText(condition.getString("coil_id"))) {
            coil_id = condition.getString("coil_id");
        }

        // 热卷号
        String hot_id = null;
        if (StringUtils.hasText(condition.getString("hot_id"))) {
            hot_id = condition.getString("hot_id");
        }

        String dbegin = null;
        if (!StringUtils.isEmpty(condition.get("dbegin"))) {
            dbegin = condition.get("dbegin").toString();
        }
        String dend = null;
        if (!StringUtils.isEmpty(condition.get("dend"))) {
            dend = condition.get("dend").toString();
        }

        String ex_thk1 = null;  //产品厚度----开始
        if (!StringUtils.isEmpty(condition.get("ex_thk1"))) {
            ex_thk1 = condition.get("ex_thk1").toString();
        }

        String ex_thk2 = null;  //产品厚度---结束
        if (!StringUtils.isEmpty(condition.get("ex_thk2"))) {
            ex_thk2 = condition.get("ex_thk2").toString();
        }

        String en_wid1 = null;  //原料宽度----开始
        if (!StringUtils.isEmpty(condition.get("en_wid1"))) {
            en_wid1 = condition.get("en_wid1").toString();
        }


        String en_wid2 = null;  //原料宽度----结束
        if (!StringUtils.isEmpty(condition.get("en_wid2"))) {
            en_wid2 = condition.get("en_wid2").toString();
        }

        String en_thk1 = null;  //原料厚度----开始
        if (!StringUtils.isEmpty(condition.get("en_thk1"))) {
            en_thk1 = condition.get("en_thk1").toString();
        }

        String en_thk2 = null;  //原料厚度----结束
        if (!StringUtils.isEmpty(condition.get("en_thk2"))) {
            en_thk2 = condition.get("en_thk2").toString();
        }

        String ele_si1 = null;  //硅含量---开始
        if (!StringUtils.isEmpty(condition.get("ele_si1"))) {
            ele_si1 = condition.get("ele_si1").toString();
        }
        String ele_si2 = null;  //硅含量---结束
        if (!StringUtils.isEmpty(condition.get("ele_si2"))) {
            ele_si2 = condition.get("ele_si2").toString();
        }

        String steelgrade = null;  //钢种
        if (!StringUtils.isEmpty(condition.get("steelgrade"))) {
            steelgrade = condition.get("steelgrade").toString();
        }

        String shift = null;
        if (!StringUtils.isEmpty(condition.get("shift"))) {
            shift = condition.get("shift").toString();
        }

        String crew = null;
        if (!StringUtils.isEmpty(condition.get("crew"))) {
            crew = condition.get("crew").toString();
        }
        // 创建表格数据集合

        List<TbDsjC621thickResult> tbDsjC621thickResult = null;
        List<ExportTbDsjC621thickResult> list = new ArrayList<>();

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            tbDsjC621thickResult = tbDsjC621thickResultDao.findDataTbDsjC621thickResultByPage((i - 1) * pageSize, i * pageSize, dbegin, dend, ex_thk1, ex_thk2, en_wid1, en_wid2, en_thk1, en_thk2, ele_si1, ele_si2, steelgrade,shift,crew,coil_id,hot_id);
            if (!tbDsjC621thickResult.isEmpty() && tbDsjC621thickResult.size() > 0) {
                for (TbDsjC621thickResult item : tbDsjC621thickResult) {
                    ExportTbDsjC621thickResult exportTbDsjC621thickResult = new ExportTbDsjC621thickResult();
                    BeanUtils.copyProperties(item,exportTbDsjC621thickResult);
                    list.add(exportTbDsjC621thickResult);
                }
            }
        }
        String classpath="";
        if(fileNames.equals("头尾厚度超差"))
        {
            classpath="classpath:exceltemplate\\头尾厚度超差.xlsx";
        }
        if(fileNames.equals("S1S4机架厚度偏差分布"))
        {
            classpath="classpath:exceltemplate\\各测厚仪偏差分布.xlsx";
        }

        try {
            Map<String,Object> data = new HashMap();
            data.put("data",list);
            File file = ResourceUtils.getFile(classpath);
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 列表导出
     * @param jsonObject
     * @param response
     */
    @Override
    public void exportold(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        String coil_id = null;
        if (StringUtils.hasText(condition.getString("coil_id"))) {
            coil_id = condition.getString("coil_id");
        }

        // 热卷号
        String hot_id = null;
        if (StringUtils.hasText(condition.getString("hot_id"))) {
            hot_id = condition.getString("hot_id");
        }

        String dbegin = null;
        if (!StringUtils.isEmpty(condition.get("dbegin"))) {
            dbegin = condition.get("dbegin").toString();
        }
        String dend = null;
        if (!StringUtils.isEmpty(condition.get("dend"))) {
            dend = condition.get("dend").toString();
        }

        String ex_thk1 = null;  //产品厚度----开始
        if (!StringUtils.isEmpty(condition.get("ex_thk1"))) {
            ex_thk1 = condition.get("ex_thk1").toString();
        }

        String ex_thk2 = null;  //产品厚度---结束
        if (!StringUtils.isEmpty(condition.get("ex_thk2"))) {
            ex_thk2 = condition.get("ex_thk2").toString();
        }

        String en_wid1 = null;  //原料宽度----开始
        if (!StringUtils.isEmpty(condition.get("en_wid1"))) {
            en_wid1 = condition.get("en_wid1").toString();
        }


        String en_wid2 = null;  //原料宽度----结束
        if (!StringUtils.isEmpty(condition.get("en_wid2"))) {
            en_wid2 = condition.get("en_wid2").toString();
        }

        String en_thk1 = null;  //原料厚度----开始
        if (!StringUtils.isEmpty(condition.get("en_thk1"))) {
            en_thk1 = condition.get("en_thk1").toString();
        }

        String en_thk2 = null;  //原料厚度----结束
        if (!StringUtils.isEmpty(condition.get("en_thk2"))) {
            en_thk2 = condition.get("en_thk2").toString();
        }

        String ele_si1 = null;  //硅含量---开始
        if (!StringUtils.isEmpty(condition.get("ele_si1"))) {
            ele_si1 = condition.get("ele_si1").toString();
        }
        String ele_si2 = null;  //硅含量---结束
        if (!StringUtils.isEmpty(condition.get("ele_si2"))) {
            ele_si2 = condition.get("ele_si2").toString();
        }

        String steelgrade = null;  //钢种
        if (!StringUtils.isEmpty(condition.get("steelgrade"))) {
            steelgrade = condition.get("steelgrade").toString();
        }

        String shift = null;
        if (!StringUtils.isEmpty(condition.get("shift"))) {
            shift = condition.get("shift").toString();
        }

        String crew = null;
        if (!StringUtils.isEmpty(condition.get("crew"))) {
            crew = condition.get("crew").toString();
        }
        // 创建表格数据集合
        List<TbDsjC621thickResult> listExcel = new ArrayList<>();// 调用service查询出数据
        List<TbDsjC621thickResult> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbDsjC621thickResultDao.findDataTbDsjC621thickResultByPage((i - 1) * pageSize, i * pageSize, dbegin, dend, ex_thk1, ex_thk2, en_wid1, en_wid2, en_thk1, en_thk2, ele_si1, ele_si2, steelgrade,shift,crew,coil_id,hot_id);
            if (!data.isEmpty() && data.size() > 0) {
                for (TbDsjC621thickResult item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        ExcelUtils.downloadExcel(fileNames,response,TbDsjC621thickResult.class,listExcel);
    }
    /**
     * 根据主键查询单条记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbDsjC621thickResultByIndocno(String data) {
        try {
            JTbDsjC621thickResult jtbDsjC621thickResult = JSON.parseObject(data, JTbDsjC621thickResult.class);
            Long indocno = jtbDsjC621thickResult.getIndocno();

            TbDsjC621thickResult tbDsjC621thickResult = tbDsjC621thickResultDao.findDataTbDsjC621thickResultByIndocno(indocno);
            return ResultData.ResultDataSuccess(tbDsjC621thickResult);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 查看记录
     *
     * @return list 对象集合返回
     */
    public List<TbDsjC621thickResult> findDataTbDsjC621thickResult() {
        List<TbDsjC621thickResult> list = tbDsjC621thickResultDao.findDataTbDsjC621thickResult();
        return list;
    }


    /**
     * 根据卷号查询对应长度归档数据曲线
     *
     * @param volumeNo
     * @return
     */
    public ResultData findThickByVolumeNo621(List<String> volumeNo) {
        if (CollectionUtils.isEmpty(volumeNo)) {
            return ResultData.ResultDataFaultSelf(null, "传入卷号为空");
        }
        HashMap<String, List<Double>> map = new HashMap<>();
        for (String no : volumeNo) {
            Query query = new Query(Criteria.where("extid").is(no));
            JDataSave621 jDataSave621 = mongoTemplate.findOne(query, JDataSave621.class);
            //边降表里的数据
            TbEdge tbEdge = mongoTemplate.findOne(query, TbEdge.class);
            List<Double> list2 = null;
            List<Double> list3 = null;
            List<Double> list4 = null;
            List<Double> list5 = null;

            if(!ObjectUtils.isEmpty(jDataSave621.getRow().get("exs4_guage_thk"))){
                list2 = jDataSave621.getRow().get("exs4_guage_thk");
                list3 = jDataSave621.getRow().get("c621_tcm_speed_s4");
                list4 = jDataSave621.getRow().get("edge_thick_center");
                list5 = jDataSave621.getRow().get("using_thick_dev");
            }else if(!ObjectUtils.isEmpty(tbEdge)){
                list2 = (List<Double>)tbEdge.getEdge().get("exs4_guage_thk");
                list3 = (List<Double>)tbEdge.getEdge().get("c621_tcm_speed_s4");
                list4 = (List<Double>)tbEdge.getEdge().get("edge_thick_center");
                list5 = (List<Double>)tbEdge.getEdge().get("using_thick_dev");
            }

//			List<Double> list = jDataSave621.getRow().get("c621_pl_pl1_level_correction_realtime");\
            //先注释
//            map.put(no+"c621_tcm_exs4_thk_gaugedev1", list1);
//            map.put(no+"exs4_guage_thk", list2);
            map.put(no, list2);
            map.put("c621_tcm_speed_s4", list3);
            map.put("edge_thick_center", list4);
            map.put("using_thick_dev", list5);
        }
        if (!CollectionUtils.isEmpty(map)) {
            return ResultData.ResultDataSuccess(map);
        }
        return ResultData.ResultDataFaultSelf(null, "查询数据为空");
    }


    /**
     * 根据卷号查询对应长度归档数据曲线
     *
     * @param volumeNo
     * @return
     */
    public List<SiData3> findThickByVolumeNo621Si3(List<String> volumeNo) {
        if (CollectionUtils.isEmpty(volumeNo)) {
            return null;
        }
        ///////////////////////////
        List<SiData3> list = new ArrayList<>();
        Query query1 = new Query(Criteria.where("extid").in(volumeNo));
        List<JDataSave621> jDataSave621_1 = mongoTemplate.find(query1, JDataSave621.class);
        List<TbEdge> tbEdges = mongoTemplate.find(query1, TbEdge.class);
        for (JDataSave621 jDataSave621 : jDataSave621_1) {
            SiData3 siData3 = new SiData3();
            List<Double> list1 = null;
            List<Double> list2 = null;
            List<Double> list3 = null;
            List<Double> list4 = null;
            List<Double> list5 = null;
            List<Double> list6 = null;
            List<Double> list7 = null;

            Double data1 = 0.0;
            Double data2 = 0.0;
            Double data3 = 0.0;
            Double data4 = 0.0;
            if (!ObjectUtils.isEmpty(jDataSave621)) {
                list1 = jDataSave621.getRow().get("c621_tcm_ens1_thk_gaugedev");
                list2 = jDataSave621.getRow().get("c621_tcm_exs1_thk_gaugedev");
                list3 = jDataSave621.getRow().get("c621_tcm_exs3_thk_gaugedev");

                list5 = jDataSave621.getRow().get("c621_tcm_exs1_thk_gaugeset");
                list6 = jDataSave621.getRow().get("c621_tcm_ens1_thk_gaugeset");
                list7 = jDataSave621.getRow().get("c621_tcm_exs3_thk_gaugeset");

//                list4 = jDataSave621.getRow().get("c621_tcm_exs4_thk_gaugedev1");


                TbEdge tbEdge = tbEdges.stream().filter(t -> t.getExtid().equals(jDataSave621.getExtid())).findFirst().orElse(null);
                if(!ObjectUtils.isEmpty(jDataSave621.getRow().get("exs4_guage_thk"))){
                    list4 = jDataSave621.getRow().get("exs4_guage_thk");
                }else {
                    list4 = (List<Double>)tbEdge.getEdge().get("exs4_guage_thk");
                }

                if (!CollectionUtils.isEmpty(list1)) {
                    data1 = list1.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
                if (!CollectionUtils.isEmpty(list2)) {
                    data2 = list2.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
                if (!CollectionUtils.isEmpty(list3)) {
                    data3 = list3.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
                if (!CollectionUtils.isEmpty(list4)) {
                    data4 = list4.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
            }

            siData3.setVolumeNo(jDataSave621.getExtid());
            siData3.setC621_tcm_ens1_thk_gaugedev(data1);
            siData3.setC621_tcm_exs1_thk_gaugedev(data2);
            siData3.setC621_tcm_exs3_thk_gaugedev(data3);
            siData3.setC621_tcm_exs4_thk_gaugedev1(data4);

            siData3.setC621_tcm_ens1_thk_gaugedevList(list1);
            siData3.setC621_tcm_exs1_thk_gaugedevList(list2);
            siData3.setC621_tcm_exs3_thk_gaugedevList(list3);
            siData3.setC621_tcm_exs4_thk_gaugedev1List(list4);

            siData3.setC621_tcm_exs3_thk_gaugeset(list7);
            siData3.setC621_tcm_ens1_thk_gaugeset(list6);
            siData3.setC621_tcm_exs1_thk_gaugeset(list5);

            list.add(siData3);
        }
        return list;
        ////////////////////////
    }

    @Override
    public void exportList(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 调用service查询出的数据
        List<TbDsjC621thickResult> tbDsjC621thickResult = null;
        List<ExportTbDsjC621thickResult> list = new ArrayList<>();
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            tbDsjC621thickResult = tbDsjC621thickResultDao.findPageData((i - 1) * pageSize, i * pageSize,condition);
            // 判断service查询出的数据集合data是否为空
            if (CollectionUtils.isEmpty(tbDsjC621thickResult)) {
                continue;
            }
            for (TbDsjC621thickResult item : tbDsjC621thickResult) {
                ExportTbDsjC621thickResult exportTbDsjC621thickResult = new ExportTbDsjC621thickResult();
                BeanUtils.copyProperties(item,exportTbDsjC621thickResult);
                list.add(exportTbDsjC621thickResult);
            }
        }
        //ExcelUtils.downloadExcel(fileNames,response,TbDsjC621thickResult.class,listExcel);
        try {
            Map<String,Object> data = new HashMap();
            data.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\厚度等级分布.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data);
            ExcelUtilsPOI.exportExcel("厚度等级分布报表",response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public  void exportListnew(JSONObject jsonObject, HttpServletResponse response){
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 创建表格数据集合
        List<TbDsjC621thickResult> listExcel = new ArrayList<>();

        // 调用service查询出的数据
        List<TbDsjC621thickResult> data = null;
        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = tbDsjC621thickResultDao.findPageData((i - 1) * pageSize, i * pageSize,condition);
            for (TbDsjC621thickResult item : data) {
                listExcel.add(item);
            }
        }
        // 判断service查询出的数据集合data是否为空
        if (CollectionUtils.isEmpty(data)) {
            return;
        }

        ExcelUtils.downloadExcel(fileNames,response,TbDsjC621thickResult.class,listExcel);
    }

    public List<SiData3> findThickByVolumeNo621Si3_1(List<String> volumeNo) {
        if (CollectionUtils.isEmpty(volumeNo)) {
            return null;
        }

        List<SiData3> list = new ArrayList<>();
        for (String no : volumeNo) {
            SiData3 siData3 = new SiData3();
            Query query = new Query(Criteria.where("extid").is(no));
            JDataSave621 jDataSave621 = mongoTemplate.findOne(query, JDataSave621.class);
            List<Double> list1 = null;
            List<Double> list2 = null;
            List<Double> list3 = null;
            List<Double> list4 = null;

            Double data1 = 0.0;
            Double data2 = 0.0;
            Double data3 = 0.0;
            Double data4 = 0.0;
            if (!ObjectUtils.isEmpty(jDataSave621)) {
                list1 = jDataSave621.getRow().get("c621_tcm_ens1_thk_gaugedev");
                list2 = jDataSave621.getRow().get("c621_tcm_exs1_thk_gaugedev");
                list3 = jDataSave621.getRow().get("c621_tcm_exs3_thk_gaugedev");
                list4 = jDataSave621.getRow().get("c621_tcm_exs4_thk_gaugedev1");

                if (!CollectionUtils.isEmpty(list1)) {
                    data1 = list1.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
                if (!CollectionUtils.isEmpty(list1)) {
                    data2 = list2.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
                if (!CollectionUtils.isEmpty(list1)) {
                    data3 = list3.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
                if (!CollectionUtils.isEmpty(list1)) {
                    data4 = list4.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
                }
            }

            siData3.setVolumeNo(no);
            siData3.setC621_tcm_ens1_thk_gaugedev(data1);
            siData3.setC621_tcm_exs1_thk_gaugedev(data2);
            siData3.setC621_tcm_exs3_thk_gaugedev(data3);
            siData3.setC621_tcm_exs4_thk_gaugedev1(data4);

            siData3.setC621_tcm_ens1_thk_gaugedevList(list1);
            siData3.setC621_tcm_exs1_thk_gaugedevList(list2);
            siData3.setC621_tcm_exs3_thk_gaugedevList(list3);
            siData3.setC621_tcm_exs4_thk_gaugedev1List(list4);
            list.add(siData3);
        }
        return list;
    }

}
